package com.feature50.clarity.decorator;

import com.feature50.clarity.Form;
import com.feature50.clarity.css.CSSFormDecorator;
import com.feature50.util.SwingUtils;

import javax.swing.JComponent;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class FormDecorators {
    private static FormDecorators instance;

    private List<FormDecorator> decorators = new ArrayList<FormDecorator>();

    private FormDecorators() {
        addDecorator(new CSSFormDecorator());
    }

    public void addDecorator(FormDecorator formDecorator) {
        decorators.add(formDecorator);
    }

    public void removeDecorator(FormDecorator formDecorator) {
        decorators.remove(formDecorator);
    }

    public void decorate(Form form) {
        List<JComponent> all = Collections.unmodifiableList(SwingUtils.getAllJComponents(form.getFormComponent()));
        for (int i = 0; i < decorators.size(); i++) {
            decorators.get(i).decorate(form, all);
        }
    }

    public static synchronized FormDecorators getInstance() {
        if (instance == null) {
            instance = new FormDecorators();
        }

        return instance;
    }
}
